import axios from 'axios'
import Qs from 'qs'
import config from '../config.js'

axios.defaults.timeout = 30000
//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'

const { app_id, version } = config

const request = axios.create({
  baseURL: config.host + '/v5_0/ai/',
  params: {},
  paramsSerializer: function(params) {
    params = params || {}
    Object.assign(params, {
      app_id: app_id,
      version
    })
    return Qs.stringify(params)
  },
  adapter: function(config) {
    let key = axios.getUri(config)
    return new Promise((resolve, reject) => {
      //let data = config.method === 'get' ? '' : Qs.stringify(config.data)
      // wx小程序 发起请求相应 log 就可以看到熟悉的返回啦
      if (config.data) {
        config.data = Object.assign(JSON.parse(config.data), {
          app_id,
          version
        })
      }
      let header = {}
      let token = ''
      if (token) {
        header['Authorization'] = token
      }
      uni.request({
        url: axios.getUri(config),
        method: config.method,
        data: config.data,
        header,
        success: (res) => {
          return resolve(res)
        },
        fail: (err) => {
          return reject(err)
        }
      })
    })
  }
})

// 请求拦截器
request.interceptors.request.use(
  function(request) {
    // request.headers.token = 'token=11124654654687';
    // console.log(request) // 请求成功
    return request
  },
  function(error) {
    // console.log(error); // 请求失败
    return Promise.reject(error)
  }
)

// 添加响应拦截器
request.interceptors.response.use(
  function(response) {
    //console.log(response.data.data) // 响应成功
    if (response.statusCode === 200) {
      return Promise.resolve(response)
    } else {
      uni.showToast({
        title: JSON.stringify(response.data),
        icon: 'none'
      })
      return Promise.reject()
    }
  },
  function(responseError) {
    // console.log(error); // 响应失败
    uni.showToast({
      title: JSON.stringify(responseError.data),
      icon: 'none'
    })
    return Promise.reject(responseError)
  }
)

export { request }
